<!DOCTYPE html>
<html>
<head>
  <title>Energia Reference - Tutorials </title>
  <link rel="shortcut icon" type="image/x-icon" href="http://energia.nu/img/eicon.png">
  <link rel='stylesheet' href='energiaWideRender.css' 
type='text/css' />
    <!--HeaderText--><style type='text/css'></style>  
  <meta name='robots' content='index,follow' />
  <meta name="Author" content="Robert Wessels" />
  <meta name="Publisher" content="Energia" />
  <meta name="Keywords" content="Energia, Wiring, Processing, Maker, LaunchPad, Arduino, Texas Instruments, Sketch, MSP430, mspgcc, mpsdebug, Electronic Arts, Programming, C, C++, Robert Wessels" />
  <meta name="Description" content="Energia is a rapid prototyping platform for the Texas Instruments MCU Launchpad. Energia is based on Wiring and Arduino and uses the Processing IDE." />
  <meta name="Copyright" content="All contents copyright Robert Wessels" />

</head>
<body>
<div id="page">
  <!--PageHeaderFmt-->
  <div id="pageheader">
    <div class="title"><a href='http://energia.nu'>Energia</a></div>
  </div>
  <!--/PageHeaderFmt-->

  <!--PageLeftFmt-->
  <div id="pagenav">
    <div id="navbar">
  	<p><a class='wikilink' href='http://energia.nu/'>Home</a>
<a class='wikilink' href='http://energia.nu/download'>Download</a>
<a class='wikilink' href='Guide_index.html'>Getting Started</a>
<a class='wikilink' href='index.html'>Reference</a>
<a class="wikilink" href="http://energia.nu/help.html">Getting Help</a>
<a class="nav" href="http://energia.nu/faq.html">FAQ</a>
<a class="wikilink" href="http://energia.nu/projects.html">Projects Using Energia</a>
<a class="nav" href="http://energia.nu/contact.html">Contact Us</a>
</p>


<p class='vspace'></p>

    </div>
  </div>
  <!--/PageLeftFmt-->

  <div id="pagetext">
  <!--PageText-->
<div id='wikitext'>
<p><em>Examples &gt; Communication</em>
</p>
<p class='vspace'></p><h3>Virtual Color Mixer</h3>
<p>This example demonstrates how to send multiple values from the MSP430 LaunchPad to the computer. The readings from three potentiometers are used to set the red, green, and blue components of the background color of a Processing sketch. 
</p>
<p class='vspace'></p><div  class='BOM' >
<h3>Hardware Required</h3>
<ul><li>MSP-EXP430G2 LaunchPad
</li><li>Three potentiometers
</li><li>Three 10K ohm resistors
</li><li>breadboard
</li><li>hook-up wire
</li></ul><p class='vspace'></p></div>
<h3>Relevant Groundwork</h3>
<p class='vspace'></p><p>The sensor values are sent from the LaunchPad to the computer as <a href='http://www.tigoe.net/pcomp/code/communication/interpreting-serial-data-bytes'>ASCII-encoded decimal numbers</a>. This means that each number is sent using the ASCII characters "0" through "9". For the value "234" for example, three bytes are sent: ASCII "2" (binary value 50), ASCII "3" (binary value 51), and ASCII "4" (binary value 52).
</p>
<p>Connect analog sensors to analog input pins A0, A1, and A2. This circuit uses three <a href='http://www.tigoe.net/pcomp/code/input-output/analog-input'>voltage divider</a> sub-circuits to generate analog voltages from the force-sensing resistors. A voltage divider has two resistors in series, dividing the voltage proportionally to their values. 
</p>
<p class='vspace'></p><h3>Circuit</h3>
<div  class='circuit' >
<p class='vspace'></p><div><img height='400px' src='../reference/img/VirtualColorMixer_bb.png' alt='' title='' /></div>
<p class='vspace'></p><p><span style='font-size:83%'>image developed using <a class='urllink' href='http://www.fritzing.org' rel='nofollow'>Fritzing</a>. For more circuit examples, see the <a class='urllink' href='http://fritzing.org/projects/' rel='nofollow'>Fritzing project page</a> </span>
</p>
<p class='vspace'></p></div>
<p class='vspace'></p><h3>Schematic</h3>
<div  class='circuit' >
<div><img height='400px' src='../reference/img/VirtualColorMixer_schem.png' alt='' title='' /></div>
</div>
<p class='vspace'></p><h3>Code Explanation</h3>
<h4>Energia</h4>
<p>The Energia code is straightforward for the most part.  We first initialize our variables named redPin, greenPin, and bluePin to read the analog sensor attached to the specific pin.  The setup() section begins the Serial communication.  The loop() section we display the values from the analog signal on the Serial Monitor.  We can check to see if the analog sensors are sending signals to the LaunchPad.  
</p>
<h4>Processing</h4>
<p>In Processing, the code generates a window that will change color according to the values set from the analog sensors.  Each analog sensor is tied to a Red, Green, or Blue color and when the sensors are changed, the three colors are mixed together and the output of the sensor values will change the color on the Processing window.   
</p>
<p class='vspace'></p><h3>Code</h3>
<pre>
/*
  This example reads three analog sensors (potentiometers are easiest)
  and sends their values serially. The Processing code at the bottom
  take those three values and use them to change the background color of the screen.

  The circuit:
  * potentiometers attached to analog inputs 0, 1, and 2


  created 2 Dec 2006
  by David A. Mellis
  modified 30 Aug 2011
  by Tom Igoe and Scott Fitzgerald
  modified 16 April 2013
  by Sean Alvarado

  This example code is in the public domain.
*/

const int redPin = A0;  // sensor to control red color
const int greenPin = A1; // sensor to control green color
const int bluePin = A2; // sensor to control blue color

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.print(analogRead(redPin));
  Serial.print(",");
  Serial.print(analogRead(greenPin));
  Serial.print(",");
  Serial.println(analogRead(bluePin));
}

/* Processing code for this example

// This example code is in the public domain.

import processing.serial.*;

float redValue = 0; // red value
float greenValue = 0; // green value
float blueValue = 0; // blue value

Serial myPort;

void setup() {
  size(200, 200);

  // List all the available serial ports
  println(Serial.list());

  // Open whatever port is the one you're using with the LaunchPad
  myPort = new Serial(this, Serial.list()[0], 9600);

  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');
}

void draw() {
  // set the background color with the color values:
  background(redValue, greenValue, blueValue);
}

void serialEvent(Serial myPort) { 
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');

  if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);
    // split the string on the commas and convert the 
	// resulting substrings into an integer array:
	float[] colors = float(split(inString, ","));
	// if the array has at least three elements, you know
	// you got the whole thing. Put the numbers in the
	// color variables:
	if (colors.length >=3) {
	  // map them to the range 0-255:
	  redValue = map(colors[0], 0, 1023, 0, 255);
	  greenValue = map(colors[1], 0, 1023, 0, 255);
	  blueValue = map(colors[2], 0, 1023, 0, 255);
    }
  }
}
*/
</pre>
<h3>Working Video</h3>
(Insert Video Here)
<h3>Try it out:</h3>

<p class='vspace'></p><h3>See Also:</h3>
<ul><li><a class='wikilink' href='Serial.html'>serial</a>()
</li><li><a class='wikilink' href='Serial_Begin.html'>serial.begin</a>()
</li><li><a class='wikilink' href='Serial_Print.html'>serial.print</a>()
</li><li><a class='wikilink' href='If.html'>if</a>()
</li><li><a class='wikilink' href='Tutorial_Dimmer.html'>Dimmer</a> - move the mouse to change the brightness of an LED.
</li><li><a class='wikilink' href='Tutorial_Graph.html'>Graph</a> - send data to the computer and graph it in Processing.
</li><li><a class='wikilink' href='Tutorial_SerialCallResponse.html'>Serial Call Response</a> - send multiple variables using a call and response (handshaking) method.
</li><li><a class='wikilink' href='Tutorial_SerialCallResponseASCII.html'>Serial Call and Response ASCII</a> - send multiple vairables using a call-and-response (handshaking) method, and ASCII-encoding the values before sending.
</li><li><a class='wikilink' href='Tutorial_SwitchCase2.html'>Serial Input (Switch (case) Statement)</a> - How to take different actions based 0n characters received by the serial port. 
</li></ul><p class='vspace'></p>
<p class='vspace'></p><p><a class='wikilink' href='Guide_index.html'>Getting Started Home</a>
</p>
<p class='vspace'></p><p><em>Corrections, suggestions, and new documentation should be posted to the <a class='urllink' href='http://forum.43oh.com/forum/28-energia/' rel='nofollow'>Forum</a>.</em>
</p>
<p class='vspace'></p><p>The text of the Energia reference is licensed under a
<a class='urllink' href='http://creativecommons.org/licenses/by-sa/3.0/' rel='nofollow'>Creative Commons Attribution-ShareAlike 3.0 License</a>.  Energia reference is based on the Arduino reference. Code samples in the reference are released into the public domain.
</p>
</div>


  </div>

  <!--PageFooterFmt-->
  <div id="pagefooter">
&copy;Energia | 
  <a href='#'>Edit Page</a> | <a href='#'>Page History</a> | <a href=#' target='_blank'>Printable View</a> | <a href='#'>All Recent Site Changes</a>
  </div>
  <!--/PageFooterFmt-->

</div>
</body>
</html>
